﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Media;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SkypeConsole
{
    class GoogleSearch
    {
        public static WebClient wclient;

        public GoogleSearch()
        {
            wclient = new WebClient();
        }

        public string[] search(string query)
        {
            string data = wclient.DownloadString("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&q=" + query + "&start=0");

            JObject json = (JObject)JsonConvert.DeserializeObject(data);

            try
            {
                JToken token = json["responseData"]["results"].First;
                string[] result = { token.Value<string>("url").ToString(), token.Value<string>("title").ToString(), token.Value<string>("content").ToString() };
                return result;
            }
            catch (Exception e)
            {
                string[] result = { e.ToString() };
                return result;
            }

        }
    }
}
